package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonPage;
import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.HospitalDoctorDTO;
import com.btpj.hospital_server.dto.param.HospitalDoctorParam;
import com.btpj.hospital_server.service.HospitalDoctorService;
import com.btpj.hospital_server.service.HospitalOutpatientService;
import com.btpj.hospital_server.service.HospitalSpecialService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "医生信息接口")
@RestController
@RequestMapping("/doctor")
public class HospitalDoctorController {

    @Resource
    HospitalDoctorService hospitalDoctorService;

    @Resource
    HospitalSpecialService hospitalSpecialService;

    @Resource
    HospitalOutpatientService hospitalOutpatientService;

    @PostMapping("/add")
    @ApiOperation("添加医生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", required = true, value = "医生姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", required = true, value = "医生性别", paramType = "query", dataType = "Int")
    })
    public CommonResult<Void> addDoctor(HospitalDoctorParam param) {
        if (param.getName() == null || param.getName().trim().isEmpty()) {
            return CommonResult.error("医生姓名不能为空");
        }

        if (param.getGender() != 1 && param.getGender() != 2) {
            return CommonResult.error("性别参数错误");
        }

        if (!hospitalSpecialService.existSpecial(param.getSpecialId())) {
            return CommonResult.error("不存在该专科编号");
        }

        if (!hospitalOutpatientService.existOutpatient(param.getOutpatientId())) {
            return CommonResult.error("不存在该门诊编号");
        }
        if (hospitalDoctorService.addDoctor(param)) {
            return CommonResult.success("添加医生信息成功");
        }
        return CommonResult.error("服务器错误，添加医生信息失败");
    }

    @PutMapping("/update/{id}")
    @ApiOperation("更新医生信息")
    @ApiImplicitParam(name = "id", required = true, value = "医生编号", paramType = "path", dataType = "Long")
    public CommonResult<Void> updateDoctor(@PathVariable Long id, HospitalDoctorParam param) {
        if (!hospitalDoctorService.existDoctor(id)) {
            return CommonResult.error("不存在指定编号的医生");
        }

        if (param.getName() != null && param.getName().trim().isEmpty()) {
            return CommonResult.error("医生姓名不能为空");
        }

        if (param.getGender() != null && param.getGender() != 1 && param.getGender() != 2) {
            return CommonResult.error("性别参数错误");
        }

        if (param.getSpecialId() != null && !hospitalSpecialService.existSpecial(param.getSpecialId())) {
            return CommonResult.error("不存在该专科编号");
        }

        if (param.getOutpatientId() != null && !hospitalOutpatientService.existOutpatient(param.getOutpatientId())) {
            return CommonResult.error("不存在该门诊编号");
        }

        if (hospitalDoctorService.updateDoctor(id, param)) {
            return CommonResult.success("修改医生信息成功");
        }

        return CommonResult.error("服务器错误，修改医生信息失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除医生信息")
    @ApiImplicitParam(name = "id", required = true, value = "医生编号", paramType = "path", dataType = "Long")
    public CommonResult<Void> deleteDoctor(@PathVariable Long id) {
        if (!hospitalDoctorService.existDoctor(id)) {
            return CommonResult.error("不存在指定编号的医生");
        }

        if (hospitalDoctorService.deleteDoctor(id)) {
            return CommonResult.success("删除医生信息成功");
        }

        return CommonResult.error("服务器错误，删除医生信息失败");
    }

    @DeleteMapping("/detail/{id}")
    @ApiOperation("获取医生信息")
    @ApiImplicitParam(name = "id", required = true, value = "医生编号", paramType = "path", dataType = "Long")
    public CommonResult<HospitalDoctorDTO> getDoctorDetail(@PathVariable Long id) {
        if (!hospitalDoctorService.existDoctor(id)) {
            return CommonResult.error("不存在指定编号的医生");
        }

        HospitalDoctorDTO hospitalDoctorDTO = hospitalDoctorService.getDoctorDetail(id);
        if (hospitalDoctorDTO != null) {
            return CommonResult.success(hospitalDoctorDTO);
        }

        return CommonResult.error("服务器错误，获取医生信息失败");
    }

    @GetMapping("/pageList")
    @ApiOperation("获取医生列表分页信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "医生姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "specialId", value = "专科编号", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "outpatientId", value = "门诊编号", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, paramType = "query", dataType = "Int"),
    })
    public CommonResult<CommonPage<HospitalDoctorDTO>> getDoctorPageList(@RequestParam(required = false) String name,
                                                                         @RequestParam(required = false) Long specialId,
                                                                         @RequestParam(required = false) Long outpatientId,
                                                                         @RequestParam Integer pageNo,
                                                                         @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalDoctorDTO> commonPage = CommonPage.listPage(hospitalDoctorService.getList(name, specialId, outpatientId));
        return CommonResult.success(commonPage);
    }
}
